package com.dyh.quickdemo.demos.lbdbean;

import com.dyh.quickdemo.bizDemo.syncWarehouseToWms.Action;

import java.util.*;

/*
@Author=Joey Wheeler
@Date=2020/12/30 13:37
*/
public class Macro {
    String name;

    LinkedList<Action> actions;
    //Map<Integer,Macro> fallbackMap;


    public static Macro init(String name){
        Macro macro = new Macro();
        macro.name = name;
        macro.actions = new LinkedList<>();
     //   macro.fallbackMap = new HashMap<>();
        return macro;
    }

    public  Macro plan(Action... actions){
        this.actions.addAll(Arrays.asList(actions));
        return this;
    }

//    public Macro fallback(int indx,Macro macro){
//        this.fallbackMap.put(indx,macro);
//        return this;
//    }

    public LinkedList<Action> run(){

        Action action;
        /*peek null 支持*/
        while ( (action = actions.peek()) !=null){
            try {
                action.act();
            } catch (Exception e) {
                return actions;
            }
            actions.poll();//null支持
        }
        return null;
    }
}
